package com.hzya.frame.utils.help;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hzya.frame.utils.core.annotation.PrimaryName;

public class UtilHelper {
	private static Map primaryNameMap = new HashMap();
	private static Map primaryKeyFieldMap = new HashMap();

	public static List getPrimaryNameList(Class klass) {
		if (primaryNameMap.containsKey(klass.getName())) {
			return ((List) primaryNameMap.get(klass.getName()));
		}

		Field[] fields = klass.getDeclaredFields();
		if ((fields != null) && (fields.length > 0)) {
			List fieldAnnotationList = new ArrayList();
			for (int i = 0; i < fields.length; ++i) {
				Field field = fields[i];
				if (field.isAnnotationPresent(PrimaryName.class))
					fieldAnnotationList.add(field);
			}

			if ((fieldAnnotationList != null) && (fieldAnnotationList.size() > 0)) {
				primaryNameMap.put(klass.getName(), fieldAnnotationList);
				return fieldAnnotationList;
			}
			primaryNameMap.put(klass.getName(), null);
			return null;
		}

		primaryNameMap.put(klass.getName(), null);
		return null;
	}

	public static String getPrimaryKey(Class klass) {
		if (primaryKeyFieldMap.containsKey(klass.getName())) {
			return ((String) primaryKeyFieldMap.get(klass.getName()));
		}

		Field[] fields = klass.getDeclaredFields();
		if ((fields != null) && (fields.length > 0)) {
			for (int i = 0; i < fields.length; ++i) {
				Field field = fields[i];
				if (field.isAnnotationPresent(PrimaryName.class)) {
					primaryKeyFieldMap.put(klass.getName(), field.getName());
					return field.getName();
				}
			}

			primaryKeyFieldMap.put(klass.getName(), null);
			return null;
		}
		primaryKeyFieldMap.put(klass.getName(), null);
		return null;
	}
}